import pymysql
from pyecharts.charts import Bar
from pyecharts import options

# 连接数据库
db = pymysql.connect(
    host="localhost",
    user="root",
    database="crawlerdemo",
    password="123456",
    charset="utf8"
)
cursor = db.cursor()

# SQL查询语句，获取每个城市的最低薪水
sql = "SELECT job_place, MIN(job_sal) as min_sal FROM job GROUP BY job_place ORDER BY min_sal"

min_sal = []
city = []

try:
    cursor.execute(sql)
    res = cursor.fetchall()
    for i in res:
        min_sal.append(i[1])  # 最低薪水
        city.append(i[0])  # 工作地点
except Exception as e:
    print("查询出问题了:", e)

# 创建柱状图
bar = Bar()

# 添加x轴的数据
bar.add_xaxis(city)

# 添加y轴的数据，并设置y轴数据的单位
bar.add_yaxis("最低薪水/元", min_sal)

# 设置全局配置项
bar.set_global_opts(
    title_opts=options.TitleOpts(title="城市最低薪水排名", subtitle="单位/元")
)
# 渲染图表到HTML文件
bar.render("barmin.html")